package com.googlecode.bindyj.property;

import com.googlecode.bindyj.property.watcher.WatcherProvider;

public class NestedProperty extends AbstractProperty {
  private final Property parent;

  private final Property child;

  public NestedProperty(Property parent, Property child) {
    this.parent = parent;
    this.child = child;
  }

  public Object get(Object source) {
    Object parentValue = parent.get(source);
    return child.get(parentValue);
  }

  public void set(Object source, Object value) {
    Object parentValue = parent.get(source);
    if (parentValue != null) {
      child.set(parentValue, value);
    }
  }

  public WatcherProvider watcherProvider(WatcherProvider childProvider) {
    return parent.watcherProvider(child.watcherProvider(childProvider));
  }

}
